Xbasic

create_table_sql Function

Syntax

result as P = create_table_sql(fieldList as C, connectionString as C, tableName as C[, mode as C])

Arguments

fieldListCharacter

A crlf string or a JSON string defining the fields in the table to be created or a file - .csv, .txt, or Excel (.xls or .xlsx) - that defines the fields to create. See below for more information.

connectionStringCharacter

The connection string to the database where you want to create the SQL table. Can be either a named connection string, or an explicit connection string.

tableNameCharacter

Name of the table to create.

modeCharacter

Can be "skip" - does not create table if there is an existing table, or "overwrite" - overwrite any existing table

Returns

resultPointer

Returns an Object with these properties:

hasErrorLogical

.T. indicates an error occurred, otherwise .F.

errorTextCharacter

Description of the error if any occurred.

sqlStatementCharacter

The SQL statement that was generated to create the table.

Description

Create a table in a SQL database.

Discussion

The fieldList parameter can be either a JSON array of objects defining each field, or a crlf delimited list of pipe delimited properties.

Example: CRLF Data Format

Example of a CRLF delimited fieldList:

id|N|6|0|AutoIncrement
name|c|20
dob|d
notes|m

In this example a primary key (that is not an AutoIncrement fields) is defined

id|c|6||PrimaryKey
name|c|20
dob|d
notes|m

When defining a primary key you can designate multiple columns:

firstname|c|20||PrimaryKey
lastname|c|20||PrimaryKey
dob|d
notes|m

Example: JSON Data Format

Example of a JSON fieldList:

[
    {"name": "id", "type": "numeric", "size": 6, "decimals" : 0, "autoIncrement": true},
    {"name": "name", "type": "character", "size": 20},
    {"name": "dob", "type": "date"},
    {"name": "notes", "type": "memo"}
]

In this next example a primary key (that is not an Auto Increment field) is defined:

[
    {"name": "id", "type": "character", "size": 6,  "primaryKey": true},
    {"name": "name", "type": "character", "size": 20},
    {"name": "dob", "type": "date"},
    {"name": "notes", "type": "memo"}
]
Multiple fields can have the 'primaryKey' attribute set to true.

Example: Excel file

dim file as c = "C:\path\to\sampleExcelData.xlsx"

dim result as p

result = create_table_sql(file,"::Name::conn","Customers2")

? result.hasError
= .F.

? result.sqlstatement
= CREATE TABLE Customers2
(
CUSTOMERID	char(255)	NULL,
COMPANYNAME	char(255)	NULL,
CONTACTNAME	char(255)	NULL,
CONTACTTITLE	char(255)	NULL,
ADDRESS	char(255)	NULL,
CITY	char(255)	NULL,
REGION	char(255)	NULL,
POSTALCODE	char(255)	NULL,
COUNTRY	char(255)	NULL,
PHONE	char(255)	NULL,
FAX	char(255)	NULL)